#include "stdafx.h"

// Mario Headers
#include "GameMain.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Mario global variables =================
CGameMain*	gGameMain;
CMySprite*	pbmTiles;

bool		canRight;
bool		canLeft;
int			jumpPos;
bool		isJump;
bool		sound;
bool		life;
vector<string>	mapMatrix;

HWND hWnd;
PAINTSTRUCT ps;
HDC hdc;
HDC hdcDouble;
HBITMAP hdcBitmap;
HBITMAP bmOld;
RECT rect;
// ========================================



// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

// My unprocess function =====================================
void OnCreate(HWND hWnd)
{
	jumpPos = 0;
	canRight = true;
	canLeft	= true;
	sound = true;
}
void OnKeyUp(WPARAM wParam)
{
	switch (wParam) {
				case VK_LEFT:
					gGameMain->KeyReleased(LEFT);
					break;
				case VK_UP:
					gGameMain->KeyReleased(UP);
					break;
				case VK_RIGHT:
					gGameMain->KeyReleased(RIGHT);
					break;
				case VK_DOWN:
					gGameMain->KeyReleased(DOWN);
					break;
				case VK_ESCAPE:
					gGameMain->KeyReleased(ESC);
					break;
	}
}
void OnKeyDown(HWND hWnd,WPARAM wParam)
{
	switch (wParam) {
		case 'N':
		case 'n':
			gGameMain->KeyPressed(KEY_N);
			break;
		case VK_LEFT:
			gGameMain->KeyPressed(LEFT);
			break;
		case VK_UP:
			gGameMain->KeyPressed(UP);
			break;
		case VK_RIGHT:
			gGameMain->KeyPressed(RIGHT);
			break;
		case VK_DOWN:
			gGameMain->KeyPressed(DOWN);
			break;
		case VK_RETURN:
			gGameMain->KeyPressed(ENTER);
			break;
		case VK_ESCAPE:
			gGameMain->KeyPressed(ESC);
			break;
	}

}
void OnPaint(HWND hWnd)
{
	hdc = BeginPaint(hWnd,&ps);

	GetClientRect(hWnd,&rect);
	hdcDouble		= CreateCompatibleDC(hdc);
	hdcBitmap		= CreateCompatibleBitmap(hdc,500,1200);
	bmOld			= (HBITMAP)SelectObject(hdcDouble, hdcBitmap);

	gGameMain->SetHDC(&hdcDouble);
	gGameMain->SendMessage(MESSAGE_PAINT);

	BitBlt(hdc,0,0,rect.right,rect.bottom,hdcDouble,0,0,SRCCOPY);
	SelectObject(hdcDouble,bmOld);
	DeleteObject(hdcDouble);
	DeleteObject(hdcBitmap);
	EndPaint(hWnd,&ps);
}

void OnDestroy() 
{
   gGameMain->isPlaying = false;
   DeleteDC(hdcDouble);
}
// My unprocess function =====================================

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= 0;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GDIMARIO));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, 514, 438, 0, NULL, hInstance, NULL);
   if (!hWnd)
   {
      return FALSE;
   }

   // ---------------- Start gdiplus ------------------
   GdiplusStartup(&gdiToken,&gdiStartInput,NULL);
   // -------------------------------------------------

   // Init GameMain
   gGameMain = new CGameMain();
   // Init Tiles
	pbmTiles				= new CMySprite();
	pbmTiles->bmSprite		= new Bitmap(L"res\\tilesstandard.png");
	pbmTiles->GetRect(-1,-1);
	life = 3;

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;

		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_KEYDOWN: 
		OnKeyDown(hWnd,wParam);
		break;
	case WM_KEYUP: 
		OnKeyUp(wParam);	
		break;
	case WM_CREATE:
		OnCreate(hWnd);
		break;
	case WM_PAINT:			
		OnPaint(hWnd);
		break;
	case WM_DESTROY:
		OnDestroy();
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_GDIMARIO, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GDIMARIO));

	// Main message loop:
	// GameLoop

	PeekMessage(&msg,NULL,0,0,PM_NOREMOVE);

	while (gGameMain->isPlaying)
	{
		while (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
			if (msg.message == WM_QUIT) 
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		if (gGameMain->enterNextState) {
			gGameMain->SendMessage(MESSAGE_ENTER);
			gGameMain->enterNextState = false;
		}

		gGameMain->SendMessage(MESSAGE_UPDATE);		// Update
		InvalidateRect(hWnd,0,0);					// Paint

		if (gGameMain->exitCurrentState) {
			// MESSAGE EXIT inside CGameMain::SwitchState
			gGameMain->enterNextState = true;
			gGameMain->exitCurrentState = false;
		}
		::Sleep(gGameMain->timer);
			// Do your game stuff here
	}

	GdiplusShutdown(gdiToken); // Shut down gdiplus token

	return (int) msg.wParam;
}